package com.example.environment.service.impl;

import com.example.environment.pojo.AqiStandard;
import com.example.environment.pojo.mapper.AqiStandardMapper;
import com.example.environment.service.AqiService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AqiServiceImpl implements AqiService {
    private List<AqiStandard> aqiStandardList;

    @Autowired
    private AqiStandardMapper aqiStandardMapper;

    @Autowired
    public AqiServiceImpl(AqiStandardMapper aqiStandardMapper) {
        this.aqiStandardMapper = aqiStandardMapper;
    }

    @PostConstruct
    public void init() {
        this.aqiStandardList = aqiStandardMapper.selectList(null);
    }

    @Override
    public int calculateAQI(Integer pm25,Integer so2,Integer co) {
        List<AqiStandard> aqiStandards = aqiStandardList;
        System.out.println(aqiStandards);
        int aqi=0;
        // 计算各个污染物的 AQI
        aqi = Math.max(aqi, calculateIndividualAQI(pm25, aqiStandards, "spmMin", "spmMax"));
        aqi = Math.max(aqi, calculateIndividualAQI(so2, aqiStandards, "so2Min", "so2Max"));
        aqi = Math.max(aqi, calculateIndividualAQI(co, aqiStandards, "coMin", "coMax"));
        return aqi;
    }

    private int calculateIndividualAQI(Integer concentration, List<AqiStandard> aqiStandards, String minField, String maxField) {
        for (AqiStandard standard : aqiStandards) {
            try {
                int min = 0;
                int max = 0;

                // 根据传入的字段名直接访问属性
                switch (minField) {
                    case "spmMin":
                        min = standard.getSpmMin();
                        break;
                    case "so2Min":
                        min = standard.getSo2Min();
                        break;
                    case "coMin":
                        min = standard.getCoMin();
                        break;
                }

                switch (maxField) {
                    case "spmMax":
                        max = standard.getSpmMax();
                        break;
                    case "so2Max":
                        max = standard.getSo2Max();
                        break;
                    case "coMax":
                        max = standard.getCoMax();
                        break;
                    // 添加更多字段的 case 以适应不同字段名的需求
                }
                System.out.println("min"+min+"max"+max);
                if (concentration >= min && concentration <= max) {
                    return (standard.getAqiLevel() - 1) * 50 + (int) ((concentration - min) / (double) (max - min) * 50);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
    public boolean isSO2Exceeded(Integer so2) {
        return so2 <= 150;
    }
    public boolean isPM25Exceeded(Integer spm) {
        return spm <= 75;
    }
    public boolean isCOExceeded(Integer co) {
        return co <= 4;
    }


}
